package cz.acies.organizer;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import cz.acies.data.DBConnector;
import cz.acies.face.modul.ModulViewer;
import cz.acies.face.viewers.TableColumnDefine;
import cz.acies.uniql.sql.SelectQuery;
import cz.acies.uniql.sql.element.UniQLCondition;

public class Tester
{
	public static void main(String[] args) {
		try {
			Tester inst = new Tester();
			/*
			List<TableColumnDefine> colDefs = new ArrayList<TableColumnDefine>();
			colDefs.add(new TableColumnDefine("displayName", "Jméno", 150, "LEFT", "", ""));
			colDefs.add(new TableColumnDefine("address", "Adresa", 250, "LEFT", "", ""));
			colDefs.add(new TableColumnDefine("email", "Email", 80, "LEFT", "", ""));
			colDefs.add(new TableColumnDefine("mobile", "Mobil", 80, "LEFT", "", ""));
			
			String serializerClass = "cz.acies.face.viewers.TableColumnDefine";
			Object serializer = Class.forName(serializerClass).newInstance();
			GsonBuilder builder = new GsonBuilder();
			builder.registerTypeAdapter(TableColumnDefine.class, serializer);
			Gson gson = builder.create();
			
			System.out.println(""+gson.toJson(colDefs));
			*/
			DBConnector db = inst.dbinit();
			String modulId = "LX.M.CONTACT";
			ModulViewer viewer =  ModulViewer.create(modulId, db);
			//System.out.println("" + viewer);
			
			/*
			List<ModulViewer> data = new ArrayList<ModulViewer>();
			
			QueryRunner qr = new QueryRunner(db.getDataSource());
			SelectQuery sql = new SelectQuery(db.dbmd()).addFrom("gui_modul")
				.add(new UniQLCondition("modulId", "=", "'"+modulId+"'"));  
			ResultSetHandler<List<ModulViewer>> handler = new BeanListHandler<ModulViewer>(ModulViewer.class);
			data = qr.query(sql.toSQLString(), handler);
			if (!data.isEmpty()) {
				ModulViewer md = data.get(0);
				
			}
			*/
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	// -----  -----------------------------------------------------------------
	
	DBConnector dbinit() {
		DBConnector connector = null;
		try {
			/*
			// HOME
			String jdbcUrl = "jdbc:mysql://localhost:3306/organizer";
			String jdbcDriver = "org.gjt.mm.mysql.Driver";
			
			Properties props = new Properties();
			props.setProperty("user", "root");
			props.setProperty("password", "smart");
			props.setProperty("useUnicode", "true");
			props.setProperty("characterEncoding", "UTF8");
			props.setProperty("characterSetResults", "UTF8");
			*/
			// WWORK
			String jdbcUrl = "jdbc:mysql://localhost:3305/genea";
			String jdbcDriver = "org.gjt.mm.mysql.Driver";

			Properties props = new Properties();
			props.setProperty("user", "root");
			props.setProperty("password", "");
			props.setProperty("useUnicode", "true");
			props.setProperty("characterEncoding", "UTF8");
			props.setProperty("characterSetResults", "UTF8");
			
			String dialectClass = "cz.acies.uniql.dialect.MySQLDialect"; 
			
			connector = DBConnector.instance();
			connector.setUrl(jdbcUrl);
			connector.setDriver(jdbcDriver);
			connector.setDialectClass(dialectClass);
			connector.setProps(props);
			connector.initialize();
			
		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return connector;
	}

}
